<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="spring cloud+.net core搭建微服务架构：配置中心（四）前言我们项目中有很多需要配置的地方，最常见的就是各种服务URL地址，这些地址针对不同的运行环境还不一样，不管和打包还是部署都麻烦，需要非常的小心。一般配置都是存储到配置文件里面，不管多小的配置变动，都需要对应用程序进行重启，对于分布式系统来说，这是非常不可取的。所以配置中心就在这种场景孕育出来，能够适配不同的环境，正在运行的">
<meta property="og:type" content="article">
<meta property="og:title" content="springcloud+steeltoe实现微服务架构：配置中心（四）">
<meta property="og:url" content="http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/index.html">
<meta property="og:site_name" content="TmoonSite">
<meta property="og:description" content="spring cloud+.net core搭建微服务架构：配置中心（四）前言我们项目中有很多需要配置的地方，最常见的就是各种服务URL地址，这些地址针对不同的运行环境还不一样，不管和打包还是部署都麻烦，需要非常的小心。一般配置都是存储到配置文件里面，不管多小的配置变动，都需要对应用程序进行重启，对于分布式系统来说，这是非常不可取的。所以配置中心就在这种场景孕育出来，能够适配不同的环境，正在运行的">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_1.png">
<meta property="og:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_2.png">
<meta property="og:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_3.png">
<meta property="og:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_5.png">
<meta property="og:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_6.png">
<meta property="og:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_7.png">
<meta property="og:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_8.png">
<meta property="article:published_time" content="2018-10-14T00:11:54.000Z">
<meta property="article:modified_time" content="2020-01-07T10:48:14.762Z">
<meta property="article:author" content="Tmoonlight">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_1.png">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/logo2.gif" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/logo2.gif">
          
        
    
    <!-- title -->
    <title>springcloud+steeltoe实现微服务架构：配置中心（四）</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
      
<link rel="stylesheet" href="/css/rtl.css">

    
    <!-- rss -->
    
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2018/10/27/excel%E9%A1%B9%E7%9B%AE/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2018/10/13/%E5%B1%80%E9%83%A8%E6%95%85%E9%9A%9C%E7%9A%84%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&text=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&is_video=false&description=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=springcloud+steeltoe实现微服务架构：配置中心（四）&body=Check out this article: http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&name=springcloud+steeltoe实现微服务架构：配置中心（四）&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&t=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#spring-cloud-net-core搭建微服务架构：配置中心（四）"><span class="toc-number">1.</span> <span class="toc-text">spring cloud+.net core搭建微服务架构：配置中心（四）</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#前言"><span class="toc-number">2.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#介绍"><span class="toc-number">3.</span> <span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#业务场景"><span class="toc-number">4.</span> <span class="toc-text">业务场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#代码实现"><span class="toc-number">5.</span> <span class="toc-text">代码实现</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#pom-xml"><span class="toc-number">5.0.0.0.1.</span> <span class="toc-text">pom.xml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ConfigServerApplication-java"><span class="toc-number">5.0.0.0.2.</span> <span class="toc-text">ConfigServerApplication.java</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#application-properties"><span class="toc-number">5.0.0.0.3.</span> <span class="toc-text">application.properties</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#demo-dev-yml"><span class="toc-number">5.0.0.0.4.</span> <span class="toc-text">demo-dev.yml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#demo-test-yml"><span class="toc-number">5.0.0.0.5.</span> <span class="toc-text">demo-test.yml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#demo-prod-yml"><span class="toc-number">5.0.0.0.6.</span> <span class="toc-text">demo-prod.yml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#nuget引用"><span class="toc-number">5.0.0.0.7.</span> <span class="toc-text">nuget引用</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#appsettings-json"><span class="toc-number">5.0.0.0.8.</span> <span class="toc-text">appsettings.json</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Startup-cs"><span class="toc-number">5.0.0.0.9.</span> <span class="toc-text">Startup.cs</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Demo-cs"><span class="toc-number">5.0.0.0.10.</span> <span class="toc-text">Demo.cs</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ValuesController-cs"><span class="toc-number">5.0.0.0.11.</span> <span class="toc-text">ValuesController.cs</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#后记"><span class="toc-number">6.</span> <span class="toc-text">后记</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#示例代码"><span class="toc-number">7.</span> <span class="toc-text">示例代码</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#传送门"><span class="toc-number">8.</span> <span class="toc-text">传送门</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#参考资料"><span class="toc-number">9.</span> <span class="toc-text">参考资料</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        springcloud+steeltoe实现微服务架构：配置中心（四）
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">TmoonSite</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2018-10-14T00:11:54.000Z" itemprop="datePublished">2018-10-14</time>
        
      
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h1 id="spring-cloud-net-core搭建微服务架构：配置中心（四）"><a href="#spring-cloud-net-core搭建微服务架构：配置中心（四）" class="headerlink" title="spring cloud+.net core搭建微服务架构：配置中心（四）"></a><a href="https://www.cnblogs.com/longxianghui/p/7660752.html" target="_blank" rel="noopener">spring cloud+.net core搭建微服务架构：配置中心（四）</a></h1><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>我们项目中有很多需要配置的地方，最常见的就是各种服务URL地址，这些地址针对不同的运行环境还不一样，不管和打包还是部署都麻烦，需要非常的小心。一般配置都是存储到配置文件里面，不管多小的配置变动，都需要对应用程序进行重启，对于分布式系统来说，这是非常不可取的。所以配置中心就在这种场景孕育出来，能够适配不同的环境，正在运行的程序不用重启直接生效。</p>
<h1 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1><p>现在开始介绍我们今天的主角spring cloud config，我觉得它最大的优点就是可以和git做集成，使用起来非常方便。spring cloud config包含服务端和客户端，服务端提供配置的读取和配置仓库，客户端来获取配置。</p>
<blockquote>
<p>也可以使用svn或者文件来存储配置文件，我们这里只讲Git的方式</p>
</blockquote>
<h1 id="业务场景"><a href="#业务场景" class="headerlink" title="业务场景"></a>业务场景</h1><p>我们模拟一个业务场景，有一个远程配置文件我们通过应用程序获取它。</p>
<h1 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h1><p>我们需要创建2个应用程序:配置服务服务端（Java），配置服务客户端（.Net Core）和一个Github仓库。</p>
<p>使用IntelliJ IDEA创建一个spring boot项目，创建配置中心服务端，端口设置为5100</p>
<h5 id="pom-xml"><a href="#pom-xml" class="headerlink" title="pom.xml"></a>pom.xml</h5><dependency>

<p><groupId>org.springframework.cloud</groupId></p>
<p><artifactId>spring-cloud-config-server</artifactId></dependency></p>
<h5 id="ConfigServerApplication-java"><a href="#ConfigServerApplication-java" class="headerlink" title="ConfigServerApplication.java"></a>ConfigServerApplication.java</h5><p>@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication {</p>
<p>public static void main(String[] args) {</p>
<p>SpringApplication.run(ConfigServerApplication.class, args);</p>
<p>}</p>
<p>}</p>
<h5 id="application-properties"><a href="#application-properties" class="headerlink" title="application.properties"></a>application.properties</h5><p>server.port=5100</p>
<p>spring.application.name=config-server</p>
<p>#git仓库地址</p>
<p>spring.cloud.config.server.git.uri=https://<a href="http://github.com/longxianghui/configs.git" target="_blank" rel="noopener">github.com/longxianghui/configs.git</a>#git用户名和密码#spring.cloud.config.server.git.username=xxx#spring.cloud.config.server.git.password=xxx#git目录下的文件夹，多个用逗号分割#spring.cloud.config.server.git.search-paths=xxx,xxx,xxx</p>
<p>使用Github创建一个仓库，并提交3个文件，文件内容如下（注意yml格式）</p>
<h5 id="demo-dev-yml"><a href="#demo-dev-yml" class="headerlink" title="demo-dev.yml"></a>demo-dev.yml</h5><p>name: mickey</p>
<p>age: 3env: test</p>
<h5 id="demo-test-yml"><a href="#demo-test-yml" class="headerlink" title="demo-test.yml"></a>demo-test.yml</h5><p>name: fiona</p>
<p>age: 28env: test</p>
<h5 id="demo-prod-yml"><a href="#demo-prod-yml" class="headerlink" title="demo-prod.yml"></a>demo-prod.yml</h5><p>name: leo</p>
<p>age: 30env: prod</p>
<blockquote>
<p>配置文件命名规则{application}-{profile}.yml</p>
<p>支持yml和properties格式</p>
</blockquote>
<p>运行配置中心服务端</p>
<p>在浏览器输入<a href="http://localhost:5001/demo/dev" target="_blank" rel="noopener">http://localhost:5001/demo/dev</a></p>
<p><img src="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_1.png" alt=""></p>
<p>再访问<a href="http://localhost:5001/demo/test" target="_blank" rel="noopener">http://localhost:5001/demo/test</a></p>
<p><img src="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_2.png" alt=""></p>
<p>再访问<a href="http://localhost:5001/demo/prod" target="_blank" rel="noopener">http://localhost:5001/demo/prod</a></p>
<p><img src="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_3.png" alt=""></p>
<p>通过上面3个URL我们发现配置中心通过REST的方式将配置信息返回。</p>
<p>配置服务REST规则如下：</p>
<blockquote>
<p>/{application}/{profile}[/{label}]</p>
<p>/{application}-{profile}.yml</p>
<p>/{label}/{application}-{profile}.yml</p>
<p>/{application}-{profile}.properties</p>
<p>/{label}/{application}-{profile}.properties</p>
</blockquote>
<p>下面我们再看看.NET程序如何读取配置信息呢？</p>
<p>创建一个 .net core web api程序，端口5101</p>
<h5 id="nuget引用"><a href="#nuget引用" class="headerlink" title="nuget引用"></a>nuget引用</h5><PackageReference Include="Steeltoe.Extensions.Configuration.ConfigServer" Version="1.1.0" />

<h5 id="appsettings-json"><a href="#appsettings-json" class="headerlink" title="appsettings.json"></a>appsettings.json</h5><p>{</p>
<p>“spring”: {</p>
<p>“application”: {</p>
<p>“name”: “demo”//与配置文件的名称对应</p>
<p>},</p>
<p>“cloud”: {</p>
<p>“config”: {</p>
<p>“uri”: “<a href="http://localhost:5100/" target="_blank" rel="noopener">http://localhost:5100</a>“,</p>
<p>“env”: “dev” //与环境名称对应</p>
<p>}</p>
<p>}</p>
<p>},</p>
<p>“Logging”: {</p>
<p>“IncludeScopes”: false,</p>
<p>“LogLevel”: {</p>
<p>“Default”: “Warning”</p>
<p>}</p>
<p>}</p>
<p>}</p>
<h5 id="Startup-cs"><a href="#Startup-cs" class="headerlink" title="Startup.cs"></a>Startup.cs</h5><p>public Startup(IHostingEnvironment env)</p>
<p>{</p>
<p>var builder = new ConfigurationBuilder()</p>
<p>.SetBasePath(env.ContentRootPath)</p>
<p>.AddJsonFile(“appsettings.json”, optional: false, reloadOnChange: true)</p>
<p>.AddJsonFile($”appsettings.{env.EnvironmentName}.json”, optional: true)</p>
<p>.AddEnvironmentVariables()</p>
<p>.AddConfigServer(env);</p>
<p>Configuration = builder.Build();</p>
<p>}</p>
<p>public void ConfigureServices(IServiceCollection services)</p>
<p>{</p>
<p>services.AddConfigServer(Configuration);</p>
<p>// Add framework services.</p>
<p>services.AddMvc();</p>
<p>services.Configure<Demo>(Configuration);</p>
<p>}</p>
<h5 id="Demo-cs"><a href="#Demo-cs" class="headerlink" title="Demo.cs"></a>Demo.cs</h5><p>public class Demo</p>
<p>{</p>
<p>public string Name { get; set; }</p>
<p>public int Age { get; set; }</p>
<p>public string Env { get; set; }</p>
<p>}</p>
<h5 id="ValuesController-cs"><a href="#ValuesController-cs" class="headerlink" title="ValuesController.cs"></a>ValuesController.cs</h5><p>[Route(“/“)]</p>
<p>public class ValuesController : Controller</p>
<p>{</p>
<p>private readonly IConfigurationRoot _config;</p>
<p>private readonly IOptionsSnapshot<Demo> _configDemo;</p>
<p>public ValuesController(IConfigurationRoot config, IOptionsSnapshot<Demo> configDemo)</p>
<p>{</p>
<p>_config = config;</p>
<p>_configDemo = configDemo;</p>
<p>}</p>
<p>[HttpGet]</p>
<p>public Demo Get()</p>
<p>{</p>
<p>//两种方式获取配置文件的数据</p>
<p>//var demo = new Demo</p>
<p>//{</p>
<p>// Name = _config[“name”],</p>
<p>// Age = int.Parse(_config[“age”]),</p>
<p>// Env = _config[“env”]</p>
<p>//};</p>
<p>var demo = _configDemo.Value;</p>
<p>return demo;</p>
<p>}</p>
<p>}</p>
<p>运行程序 浏览器访问<a href="http://localhost:5101/" target="_blank" rel="noopener">http://localhost:5101/</a>,</p>
<p><img src="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_5.png" alt=""></p>
<p>更改配置文件appsettings.json，”env”: “test”,重新启动程序，刷新页面</p>
<p><img src="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_6.png" alt=""></p>
<p>再更改配置文件appsettings.json，”env”: “prod”,程序启动程序，刷新页面</p>
<p><img src="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_7.png" alt=""></p>
<p>这样通过修改本地的配置文件，就能获取远程上的各种配置了。</p>
<p>我们再试试修改远程的配置文件，修改demo-prod.yml的配置name: leo1,提交到github。</p>
<p>再访问<a href="http://localhost:5011/" target="_blank" rel="noopener">http://localhost:5011/</a>，发现配置并没有变化，这是因为配置服务并不知道git有更新，我们重启配置服务，再次访问，问题依旧，那么再重启客户端,发现我们得到了刚才更新的配置name= leo1，配置生效了。</p>
<p><img src="https://images.cnblogs.com/cnblogs_com/longxianghui/1095508/o_8.png" alt=""></p>
<h1 id="后记"><a href="#后记" class="headerlink" title="后记"></a>后记</h1><p>通过上面的例子，我们能够通过应用程序获取到配置信息，但是这有明显的问题，总不能一有配置更新就去重启配置服务和客户端吧？如何做到自动通知到客户端呢？留下这些问题，敬请期待下一章。</p>
<h1 id="示例代码"><a href="#示例代码" class="headerlink" title="示例代码"></a>示例代码</h1><p>所有代码均上传<a href="https://github.com/longxianghui/microservice" target="_blank" rel="noopener">github</a>。代码按照章节的顺序上传，例如第一章demo1，第二章demo2以此类推。</p>
<p>求推荐，你们的支持是我写作最大的动力，我的QQ群：328438252,交流微服务。</p>
<h1 id="传送门"><a href="#传送门" class="headerlink" title="传送门"></a>传送门</h1><ul>
<li><p><a href="http://www.cnblogs.com/longxianghui/p/7561259.html" target="_blank" rel="noopener">spring cloud+dotnet core搭建微服务架构：服务注册（一）</a></p>
</li>
<li><p><a href="http://www.cnblogs.com/longxianghui/p/7576736.html" target="_blank" rel="noopener">spring cloud+dotnet core搭建微服务架构：服务发现（二）</a></p>
</li>
<li><p><a href="http://www.cnblogs.com/longxianghui/p/7646870.html" target="_blank" rel="noopener">spring cloud+dotnet core搭建微服务架构： Api网关（三）</a></p>
</li>
<li><p><a href="http://www.cnblogs.com/longxianghui/p/7660752.html" target="_blank" rel="noopener">spring cloud+dotnet core搭建微服务架构：配置中心（四）</a></p>
</li>
</ul>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><p>java部分</p>
<ul>
<li><p><a href="https://github.com/SpringCloud/spring-cloud-docs" target="_blank" rel="noopener">spring cloud文档</a></p>
</li>
<li><p><a href="http://www.cnblogs.com/ityouknow/category/994104.html" target="_blank" rel="noopener">纯洁大神spring cloud系列</a></p>
</li>
</ul>
<p>.net部分</p>
<ul>
<li><p><a href="http://steeltoe.io/docs/" target="_blank" rel="noopener">SteeltoeOSS文档</a></p>
</li>
<li><p><a href="https://github.com/SteeltoeOSS/" target="_blank" rel="noopener">SteeltoeOSS源码</a></p>
</li>
</ul>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#spring-cloud-net-core搭建微服务架构：配置中心（四）"><span class="toc-number">1.</span> <span class="toc-text">spring cloud+.net core搭建微服务架构：配置中心（四）</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#前言"><span class="toc-number">2.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#介绍"><span class="toc-number">3.</span> <span class="toc-text">介绍</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#业务场景"><span class="toc-number">4.</span> <span class="toc-text">业务场景</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#代码实现"><span class="toc-number">5.</span> <span class="toc-text">代码实现</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#pom-xml"><span class="toc-number">5.0.0.0.1.</span> <span class="toc-text">pom.xml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ConfigServerApplication-java"><span class="toc-number">5.0.0.0.2.</span> <span class="toc-text">ConfigServerApplication.java</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#application-properties"><span class="toc-number">5.0.0.0.3.</span> <span class="toc-text">application.properties</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#demo-dev-yml"><span class="toc-number">5.0.0.0.4.</span> <span class="toc-text">demo-dev.yml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#demo-test-yml"><span class="toc-number">5.0.0.0.5.</span> <span class="toc-text">demo-test.yml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#demo-prod-yml"><span class="toc-number">5.0.0.0.6.</span> <span class="toc-text">demo-prod.yml</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#nuget引用"><span class="toc-number">5.0.0.0.7.</span> <span class="toc-text">nuget引用</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#appsettings-json"><span class="toc-number">5.0.0.0.8.</span> <span class="toc-text">appsettings.json</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Startup-cs"><span class="toc-number">5.0.0.0.9.</span> <span class="toc-text">Startup.cs</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Demo-cs"><span class="toc-number">5.0.0.0.10.</span> <span class="toc-text">Demo.cs</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#ValuesController-cs"><span class="toc-number">5.0.0.0.11.</span> <span class="toc-text">ValuesController.cs</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#后记"><span class="toc-number">6.</span> <span class="toc-text">后记</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#示例代码"><span class="toc-number">7.</span> <span class="toc-text">示例代码</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#传送门"><span class="toc-number">8.</span> <span class="toc-text">传送门</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#参考资料"><span class="toc-number">9.</span> <span class="toc-text">参考资料</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&text=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&is_video=false&description=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=springcloud+steeltoe实现微服务架构：配置中心（四）&body=Check out this article: http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&title=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&name=springcloud+steeltoe实现微服务架构：配置中心（四）&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2018/10/14/springcloud+steeltoe%E5%AE%9E%E7%8E%B0%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%EF%BC%9A%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83%EF%BC%88%E5%9B%9B%EF%BC%89/&t=springcloud+steeltoe实现微服务架构：配置中心（四）" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Tmoonlight
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->

<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">


<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">


    <!-- jquery -->

<script src="/lib/jquery/jquery.min.js"></script>


<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>

<!-- clipboard -->

  
<script src="/lib/clipboard/clipboard.min.js"></script>

  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
